const Notice = require('../models/Notice');

//获取所有信息
const getNoticeAll = async () => {
  let result = await Notice.findAll();
  if (result) {
    return {
      msg: '获取成功',
      status: 1,
      content: result,
    };
  } else {
    return {
      msg: '获取失败',
      status: 0,
    };
  }
};

//添加公告
const addNotice = async ({ time, content }) => {
  let result = await Notice.create({
    time,
    content,
  });
  if (result) {
    return {
      msg: '公告添加成功',
      status: 1,
      content,
    };
  } else {
    return {
      msg: '公告添加失败',
      status: 0,
    };
  }
};

//删除公告，根据id删除
const deleteNotice = async (id) => {
  let result = await Notice.destroy({
    where: {
      id,
    },
  });
  if (result) {
    return {
      msg: '删除成功',
      status: 1,
    };
  } else {
    return {
      msg: '失败',
      status: 0,
    };
  }
};

module.exports = {
  getNoticeAll,
  addNotice,
  deleteNotice,
};
